package com.lsfb.signln.Leave.PersonnelSelect;

import android.content.Intent;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.lsfb.signln.Login.LoginActivity;
import com.lsfb.signln.R;
import com.lsfb.signln.Utils.LittleUtils;
import com.lsfb.signln.Utils.MyProgressDialog;
import com.lsfb.signln.Utils.URLString;
import com.lsfb.signln.View.MyExpandableListView;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.DensityUtils;
import com.zgscwjm.lsfbbasetemplate.utils.T;
import com.zgscwjm.lsfbbasetemplate.view.titlebar.LsfbTitleBar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 人员选择
 * Created by Administrator on 2018/1/31 0031.
 */
@ContentView(R.layout.aty_personnel_select)
public class PersonnelSelectActivity extends LsfbActivity implements PersonnelSelectAdapter.CheckListener {

    /**
     * 我自己
     */
    @ViewInject(R.id.aty_personnel_select_my_img)
    private ImageView select_my_img;

    /**
     * 全部
     */
    @ViewInject(R.id.aty_personnel_select_all_img)
    private ImageView select_all_img;

    /**
     * 自选
     */
    @ViewInject(R.id.aty_personnel_select_optional_img)
    private ImageView select_optional_img;

    /**
     * 二级列表
     */
    @ViewInject(R.id.aty_listview)
    private MyExpandableListView listview;

    private PersonnelSelectAdapter adapter;
    private List<SelfDeparListBean.ListBean> groups;
    private List<SelfDeparListBean.ListBean.PlistBean> childs;

    private MyProgressDialog dialog;

    private int requestCode = 0;
    //创建人类型
    private String usertype = "我自己";
    //创建人
    private String users = "";
    //创建人-部门
    private String depars = "";

    @Override
    public void initView() {
        LsfbTitleBar titleBar = LittleUtils.setsimpletitlebar(this, "人员选择");
        TextView textView = LittleUtils.rightbtn(this, "确认");

        dialog = new MyProgressDialog(this, "加载中......");

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int user = 0;
                int depar = 0;

                for (int i = 0; i < groups.size(); i++) {
                    if (groups.get(i).isCheckAll) {
                        if (depar == 0) {
                            depars = String.valueOf(groups.get(i).getDid());
                            depar++;
                        } else {
                            depars = depar + "," + String.valueOf(groups.get(i).getDid());
                        }
                    } else {
                        for (int j = 0; j < groups.get(i).getPlist().size(); j++) {
                            if (groups.get(i).getPlist().get(j).isCheck) {
                                if (user == 0) {
                                    users = String.valueOf(groups.get(i).getPlist().get(j).getId());
                                    user++;
                                } else {
                                    users = users + "," + String.valueOf(groups.get(i).getPlist().get(j).getId());
                                }
                            }
                        }
                    }
                }
                Intent intent = new Intent();
                intent.putExtra("usertype", usertype);
                if (!depars.isEmpty()) {
                    intent.putExtra("depars", depars);
                }
                if (!users.isEmpty()) {
                    intent.putExtra("users", users);
                }
                if (usertype.equals("自选") && users.isEmpty() && depars.isEmpty()) {
                    T.showShort(getApplicationContext(), "请选择创建人");
                    return;
                }
                setResult(requestCode, intent);
                finish();
            }
        });
        titleBar.setRightView(textView, 0, 0, DensityUtils.dp2px(this, 10), 0);

        groups = new ArrayList<>();
        childs = new ArrayList<>();

        adapter = new PersonnelSelectAdapter(this, groups, this);
        listview.setAdapter(adapter);

//        // 清除默认的 Indicator
//        listview.setGroupIndicator(null);

        listview.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                                        int groupPosition, long id) {
                return false;
            }
        });

        listview.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                return false;
            }
        });

        listview.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                for (int i = 0; i < groups.size(); i++) {
                    if (groupPosition != i) {
                        listview.collapseGroup(i);
                    }
                }
            }
        });

        getDatas();
    }

    @Override
    public void initEvent() {

    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.aty_personnel_select_my, R.id.aty_personnel_select_all, R.id.aty_personnel_select_optional})
    public void onClick(View view) {
        switch (view.getId()) {
            //我自己
            case R.id.aty_personnel_select_my:
                select_my_img.setVisibility(View.VISIBLE);
                select_all_img.setVisibility(View.GONE);
                select_optional_img.setVisibility(View.GONE);
                listview.setVisibility(View.GONE);
                usertype = "我自己";
                break;
            //全部
            case R.id.aty_personnel_select_all:
                select_my_img.setVisibility(View.GONE);
                select_all_img.setVisibility(View.VISIBLE);
                select_optional_img.setVisibility(View.GONE);
                listview.setVisibility(View.GONE);
                usertype = "全部";
                break;
            //自选
            case R.id.aty_personnel_select_optional:
                select_my_img.setVisibility(View.GONE);
                select_all_img.setVisibility(View.GONE);
                select_optional_img.setVisibility(View.VISIBLE);
                listview.setVisibility(View.VISIBLE);
                usertype = "自选";
                break;
        }
    }

    @Override
    public void initData() {

    }

    /**
     * 得到自己名下的所有部门
     */
    private void getDatas() {
        HashMap hashMap = new HashMap();
        dialog.showdialog();
        new BaseInternet().getData_Get(URLString.GETSELFDEPARLIST, hashMap, SelfDeparListBean.class,
                new SelfDeparListEvent<SelfDeparListBean>(), true, false);
    }

    @EventAnnotation
    public void eventResult(SelfDeparListEvent<SelfDeparListBean> event) {
        dialog.hidedialog();
        switch (event.getCode()) {
            case 200:
                if (event.getData().getList() != null & !event.getData().getList().isEmpty()) {
                    groups.clear();
                    groups.addAll(event.getData().getList());
//                    if (groups.size() > 0) {
//                        for (int i = 0; i < groups.size(); i++)
//                            listview.expandGroup(i);//所有项默认展开
//                    }
                    adapter.notifyDataSetChanged();
                }
                break;
            case 1006:
                T.showShort(this, "您的账号已在其它设备登录，请重登录");
                LittleUtils.exit(this);
                startActivity(new Intent(this, LoginActivity.class));
                break;
            default:
                T.showShort(this, event.getMsg());
                break;
        }
    }

    /**
     * 二级列表全选
     */
    @Override
    public void onCheckAll(int groupPosition) {
        if (groups != null && !groups.isEmpty()) {
            groups.get(groupPosition).isCheckAll = !groups.get(groupPosition).isCheckAll;
        }
        for (SelfDeparListBean.ListBean.PlistBean data : groups.get(groupPosition).getPlist()) {
            data.isCheck = groups.get(groupPosition).isCheckAll;
        }
        adapter.notifyDataSetChanged();
    }

    /**
     * 二级列表单个选中
     */
    @Override
    public void onChild(SelfDeparListBean.ListBean.PlistBean model, int groupsPosition) {
        if (groups != null || !groups.isEmpty()) {
            model.isCheck = !model.isCheck;
        }
        int num = 0;
        for (int i = 0; i < groups.get(groupsPosition).getPlist().size(); i++) {
            if (groups.get(groupsPosition).getPlist().get(i).isCheck) {
                num++;
            }
        }
        if (num == groups.get(groupsPosition).getPlist().size()) {
            groups.get(groupsPosition).isCheckAll = true;
        } else {
            groups.get(groupsPosition).isCheckAll = false;
        }
        adapter.notifyDataSetChanged();
    }
}
